﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Com.Mapabc.Maps.Api.Overlays;
using Com.Mapabc.Maps.Api;
using LantinxShare.LBS;
using LantinxShare.Common;
using LantinxLBS.Common;
using Com.Mapabc.Maps.Api.BaseTypes;

namespace LantinxLBS.Views
{
    public partial class FrmAddMark : ChildWindow
    {
        public FrmAddMark()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            String markName = this.txtMarkName.Text.Trim();
            String address = this.txtAddress.Text.Trim();
            String description = this.txtDescription.Text.Trim();

            if (string.IsNullOrEmpty(markName))
            {
                MsgHelper.Show("标注名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(address))
            {
                MsgHelper.Show("地址不能为空");
                return;
            }
            if (string.IsNullOrEmpty(description))
            {
                MsgHelper.Show("描述不能为空");
                return;
            }

            GMark gmark = new GMark();
            gmark.MarkName = markName;
            gmark.Address = address;
            gmark.Description = description;

            gmark.Longitude = G.BLongitude(this._circle.Center.X);
            gmark.Latitude = G.BLatitude(this._circle.Center.Y);
            Point nePoint = _map.FromLngLatToContainerPixel(_circle.Bounds.NorthEast);
            Point centerPoint = _map.FromLngLatToContainerPixel(_circle.Center);

            gmark.Radius = (double)(nePoint.X - centerPoint.X) / 2.0d;

            gmark.West = G.BLongitude(this._circle.Bounds.SouthWest.X);
            gmark.South = G.BLatitude(this._circle.Bounds.SouthWest.Y);
            gmark.East = G.BLongitude(this._circle.Bounds.NorthEast.X);
            gmark.North = G.BLatitude(this._circle.Bounds.NorthEast.Y);

            SaveGMark(gmark);
        }

        private void SaveGMark(GMark gmark)
        {
            if (gmark != null)
            {

                PathHelper.GetData("AddressPage.aspx?option=" + GConstant.OP_SET_MARK,
                    new Dictionary<string, string>(){
                {"markName",gmark.MarkName},
                {"address",gmark.Address},
                {"description",gmark.Description},
                {"longitude",gmark.Longitude.ToString()},
                {"latitude",gmark.Latitude.ToString()},
                {"radius",gmark.Radius.ToString()},
                {"west",gmark.West.ToString()},
                {"south",gmark.South.ToString()},
                {"east",gmark.East.ToString()},
                {"north",gmark.North.ToString()},
                {"altitude",gmark.Altitude.ToString()},
                }, (e) =>
                {
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        gmark.Id = int.Parse(e.Result);

                        successAction(_circle, gmark);

                        this.DialogResult = true;

                    });
                });
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
                    {
                        _map.RemoveOverlay(_circle);
                    });
            this.DialogResult = false;
        }

        public void SetCircle(MCircle circle, MMap map, Action<MCircle, GMark> action)
        {
            _circle = circle;
            _map = map;
            successAction = action;
        }

        private Action<MCircle, GMark> successAction;
        private MCircle _circle;
        private MMap _map;
    }
    //public delegate
}

